#pragma once

template<typename T>
class ResourceGenerator: public IResourceGenerator
{
public:
	ResourceGenerator<T>()
	{
		std::stringstream ss;
		ss << "ResGen<" << std::string(typeid(T).name()) << ">";

		this->m_typename = ss.str();

		AssertLogI(true, "ResourceGenerator<%s> created.", this->m_typename.c_str());
		//LogI(1) << "Creating ResourceGenerator< " << typeid(T).name() << " >";
	}

	virtual ~ResourceGenerator()
	{
		//LogI(1) << "Deleting ResourceGenerator< " << typeid(T).name() << " >";

		for (std::map<std::string, T*>::iterator it = this->m_items.begin(); it != this->m_items.end(); ++it)
		{
			delete(*it).second;
		}
	}

	void Add(const std::string& name, T* nresource)
	{
		this->m_items.insert(std::make_pair(name, nresource));
	}

	T* Get(const std::string& name)
	{
		std::map<std::string, T*>::iterator it = this->m_items.find(name);

		if (it != this->m_items.end())
		{
			return it->second;
		}
		else
		{
			return NULL;
		}
	}

	void Free(const std::string& name)
	{
		std::map<std::string, T*>::iterator it = this->m_items.find(name);

		if (it != std::map::end)
		{
			this->m_items.erase(it);
		}
	}

	const std::map<std::string, T*>& GetItems() const
	{
		return this->m_items;
	}

	void ReloadAll()
	{
		for (std::map<std::string, T*>::iterator it = this->m_items.begin(); it != this->m_items.end(); ++it)
		{
			IResource* res = dynamic_cast<IResource*>(it->second);

			if (res != NULL)
			{
				res->Reload();
			}
			else
			{
				AssertLogC(true, "Not IResource interface detected");
			}
		}
	}

private:
	std::map<std::string, T*> m_items;
};